﻿using Abp.AutoMapper;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Timing;
using Abp.Zero;
using Vecen.BangChuang.Localization;
using Abp.Zero.Configuration;
using Vecen.BangChuang.MultiTenancy;
using Vecen.BangChuang.Authorization.Roles;
using Vecen.BangChuang.Authorization.Users;
using Vecen.BangChuang.Net.Sms;
using Vecen.BangChuang.Timing;
using Abp.Configuration.Startup;
using Vecen.BangChuang.SmsImpts;
using Abp.Dependency;
using Vecen.BangChuang.BangChuang.Pay;

namespace Vecen.BangChuang
{
    [DependsOn(typeof(AbpZeroCoreModule),typeof(BangChuangNetSmsModule),
        typeof(AbpAutoMapperModule))]
    public class BangChuangCoreModule : AbpModule
    {
        public override void PreInitialize() {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            //Declare entity types
            Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            Configuration.Modules.Zero().EntityTypes.User = typeof(User);

            Configuration.UnitOfWork.IsTransactional = false;
            Configuration.Modules.Zero().UserManagement.ExternalAuthenticationSources.Add<PhoneNumberAuthenticationSource>();

            BangChuangLocalizationConfigurer.Configure(Configuration.Localization);

            Configuration.Settings.Providers.Add<BangChuangSettingProvider>();
            //Enable this line to create a multi-tenant application.
            Configuration.MultiTenancy.IsEnabled = BangChuangConsts.MultiTenancyEnabled;

            Configuration.Modules.AbpAutoMapper().Configurators.Add(c => c.AddProfile<ChargeMapperProfile>());
            //Configure roles
            AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);
            #if !DEBUG
            Configuration.ReplaceService<ISmsSender,RongLianSmsSender>(DependencyLifeStyle.Transient);
            #endif

        }

        public override void Initialize() {
            IocManager.RegisterAssemblyByConvention(typeof(BangChuangCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
        }
    }
}